package com.dashu.authority.servie.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dashu.authority.common.Constant;
import com.dashu.authority.common.MessageCode;
import com.dashu.authority.entity.Role;
import com.dashu.authority.entity.UserAndRole;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.repository.RoleMapper;
import com.dashu.authority.servie.RoleService;
import com.dashu.authority.servie.UserAndRoleService;
import com.dashu.authority.util.AuthorityServiceUtil;
import com.github.pagehelper.PageHelper;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
	
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private UserAndRoleService  userAndRoleService;

	@Override
	public List<Role> queryAllByExample(Role entity, int pageNum, int pageSize) throws AuthorityException {
		PageHelper.startPage(pageNum, pageSize);
		Map<Object, Object> params = new HashMap<Object, Object>();
		if (entity!=null) {
			if (entity.getRoleName()!=null && !"".equals(entity.getRoleName())) {
				params.put("roleName", entity.getRoleName());
			}	
			if (entity.getId()!=null) {
				params.put("id", entity.getId());
			}
		}
		params.put("orderBy", "create_time");
		params.put("sort", "desc");
		List<Role> list = roleMapper.queryAllByExample(params);
		if (list!=null && list.size()>0) {
			for (Role role : list) {
				if (role.getParentId()!=null) {
					String parentName = roleMapper.findNameByRoleId(role.getParentId());
					role.setParentName(parentName);
				}
			}
		}
		return list;
	}

	@Override
	public Map<String, Object> insertEntity(Role entity) throws AuthorityException {
		int ret = roleMapper.insertEntity(entity);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}else{
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> updateEntity(Role entity) throws AuthorityException {
		int ret = roleMapper.updateEntity(entity);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}else{
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> deleteByIds(Integer[] ids) throws AuthorityException {
		if(ids==null || ids.length==0){
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(),"信息id数组:");
		}
		Map<Object,Object> params=new HashMap<Object,Object>();
		params.put("ids", ids);
		int ret=roleMapper.deleteByIds(params);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		}else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}

	@Override
	public Map<String, Object> updateStatus(Integer[] ids, String status) throws AuthorityException {
		if(ids==null || ids.length==0){
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(),"信息id数组:");
		}
		Map<Object,Object> params=new HashMap<Object,Object>();
		params.put("ids", ids);
		params.put("status", status);
		int ret=roleMapper.updateStatus(params);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		}else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}
	
	@Override
	public List<Role> queryAllByUserId(Integer userId, int pageNum, int pageSize) throws AuthorityException {
		List<UserAndRole> list = userAndRoleService.queryAllByUserId(userId);
		if (list != null && list.size() > 0) {
			List<Integer> ids = new ArrayList<Integer>();
			for (int i = 0; i < list.size(); i++) {
				ids.add(list.get(i).getRoleId());
			}
			PageHelper.startPage(pageNum, pageSize);
			Map<Object, Object> params = new HashMap<Object, Object>();
			params.put("ids", ids);
			params.put("orderBy", "create_time");
			params.put("sort", "desc");
			return roleMapper.queryAllByExample(params);
		}
		return  null;
	}
	
	//查看用户没有拥有的角色
	@Override
	public List<Role> queryAllByNoUserId(Integer userId, int pageNum, int pageSize) throws AuthorityException {
		List<UserAndRole> list = userAndRoleService.queryAllByUserId(userId);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("orderBy", "create_time");
		params.put("sort", "desc");
		PageHelper.startPage(pageNum, pageSize);
		if (list != null && list.size() > 0) {
			List<Integer> ids = new ArrayList<Integer>();
			for (int i = 0; i < list.size(); i++) {
				ids.add(list.get(i).getRoleId());
			}
			params.put("noIds", ids);
			return roleMapper.queryAllByExample(params);
		}
		return roleMapper.queryAllByExample(params);
	}

	@Override
	public Role queryById(Integer id) throws AuthorityException {
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("id", id);
		List<Role> list = roleMapper.queryAllByExample(params);
		if (list!=null && list.size()>0) {
			return list.get(0);
		}
		return null;
	}
}
